package saucaca.fragment;

import java.util.List;

import org.json.JSONException;

import saucaca.CustomView.CircularImageView;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.SingerObject;
import saucaca.Util.AppController;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.lemon.kenhnhacvn.BaseActivity;
import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

@SuppressLint("NewApi") public class ArtistDetailContent extends Fragment {
	String tag=ArtistDetailContent.class.getName();
	SingerObject singerObject;
	CircularImageView artistAvatar;
	TextView txtName,txtInfor, txtBirthday, txtSex;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	Context context;
	WebView webView;
	String singerCode;
	List<SingerObject> listSinger;
	ImageLoader imageLoader;
	public static ArtistDetailContent newInstance(SingerObject SongId) {
		ArtistDetailContent f = new ArtistDetailContent();
		Bundle args = new Bundle();
		args.putSerializable(MainActivity.ID, SongId);
		f.setArguments(args);
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.artist_details_content, container, false);
		findviewByID(android);
		Bundle b = getArguments();
		singerObject = (SingerObject) b.getSerializable(MainActivity.ID);
		this.singerCode = singerObject.getSINGER_ID();
		context=getActivity();
		try {
			imageLoader=((BaseActivity)getActivity()).imageLoader;
		} catch (Exception ex) {
			imageLoader=AppController.getInstance().getImageLoader();
		}
//		ImageLoader.getInstance().loadImage(singerObject.getAVATAR(), new SimpleImageLoadingListener() {
//			@Override
//			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//				super.onLoadingComplete(imageUri, view, loadedImage);
//				artistAvatar.setImageBitmap(loadedImage);
//			}
//		});
		imageLoader.get(singerObject.getAVATAR(), new ImageLoader.ImageListener() {
	        @Override
	        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
	        	artistAvatar.setImageBitmap(response.getBitmap());
	        }

			@Override
			public void onErrorResponse(VolleyError arg0) {
			}
	    });
		showInfoSinger(singerObject);
		new getArtistDetail().execute("");
		return android;
	}

	void findviewByID(View view) {
		artistAvatar = (CircularImageView) view.findViewById(R.id.artist_pictureInfo);
		txtName = (TextView) view.findViewById(R.id.artist_txtNameInfo);
		webView = (WebView) view.findViewById(R.id.artist_webInfo);
		webView.setWebViewClient(new WebViewClient() {

			   public void onPageFinished(WebView view, String url) {
			       	webView.setVisibility(View.VISIBLE);
			    }
			});
//		webView.setBackgroundColor(Color.TRANSPARENT);
//	    if (Build.VERSION.SDK_INT >= 11) {
//	    	webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
//	    }
		webView.setInitialScale(100);
		 txtInfor = (TextView) view.findViewById(R.id.artist_txtInfo);
		txtBirthday = (TextView) view.findViewById(R.id.artist_txtBirthday);
		txtSex = (TextView) view.findViewById(R.id.artist_txtSex);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
	}

	public void showInfoSinger(SingerObject singerObject) {
			txtName.setText(singerObject.getTXNAME());
			try {
				if (singerObject.getSHORTDESC()!=null&& !singerObject.getSHORTDESC().equals("")) {
					final String mimeType = "text/html";
					final String encoding = "utf-8";
					String data =Html.fromHtml(singerObject.getSHORTDESC()).toString();
					webView.loadDataWithBaseURL(null,data, mimeType, encoding,null);
	//				txtInfor.setText(data);
				}
			} catch (Exception e) {
			}
			
			txtBirthday.setText(singerObject.getBIRTHDAY());
			txtSex.setText(BaseEngine.getSex(singerObject.getSEX()));
	}
	class getArtistDetail extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... param) {
			String str = WServiceLib.artistDetai(singerCode);
//			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					BaseEngine.showLogformLongString(result);
					listSinger = JsonReader.getArtistDetai(result);
					if (listSinger != null && listSinger.size() > 0)
						showInfoSinger(listSinger.get(0));
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}

	}
}
